package pl.olap.sites;

import java.util.ArrayList;

public class Punksrock extends Site implements Parser {

	static final public String URL_MAIN = "http://filmy.punksrock.info/index.php";
	private int countPages = 0;
	private int iterator = 1;
	private String patternCountPages = new String(
			"Strona: </span>\\d+ z \\d+ +<");
	private String patternTitle = new String(
			"<div class='translated'>.+<div class='title'>");
	private String patternYear = new String("search=\\d\\d\\d\\d'>\\d\\d\\d\\d");
	private String patternGenre = new String("category=.+</a></td>.+Kraj:</b>");
	private String patternNationality = new String("country=.+'>.+<b>Re�yser");
	private String patternDirector = new String("Re�yser.+Czas trwania");
	private String patternDuration = new String("Czas trwania:.+min");
	private String patternCast = new String("center.+</a></td>");
	private String patternMovie = new String(
			"<div class='movie num_[0-9]+ curvy1'>.+");
	private String patternFirstDescription = new String(
			"#description_(\\d\\d\\d|\\d\\d\\d\\d|\\d\\d|\\d).+Video");
	private String patternRating = new String(
			"title='IMDB-Rating: (\\d\\.\\d|\\d)");

	public Punksrock() {

	}

	public void setCountPages(String p_content) {
		this.countPages = getCountPagesFromContent(p_content);

	}

	public Integer getCountPages() {
		return this.countPages;
	}

	public String getNextUrl() {
		iterator = iterator + 1;
		return this.URL_MAIN + "?page=" + iterator;
	}

	private int getCountPagesFromContent(String p_content) {
		String[] strings = find(p_content, patternCountPages).split("z");
		if (strings.length > 1) {
			return getNumberFromString(strings[1]);

		}
		return 0;
	}

	public String[] getMoviesContent(String p_content) {

		return p_content.split("<div class=\"tr content\"");
	}

	public String getTitle(String p_content) {
		String[] result = find(p_content, this.patternTitle).split(">");
		if (result.length < 2) {
			return "";
		}
		String[] title = result[1].split("<");
		return title[0];
	}

	public Integer getNumberFromString(String p_string) {
		return Integer.parseInt(find(p_string, new String("[0-9]+")));

	}

	public String getContentMovie(String p_content) {

		return find(p_content, patternMovie);
	}

	public Integer getYear(String p_content) {
		String[] result = find(p_content, this.patternYear).split(">");
		if (result.length < 2) {
			return 0;
		}
		return Integer.parseInt(result[1]);
	}

	@Override
	public String[] getNationality(String p_content) {
		String[] contents = find(p_content, this.patternNationality)
				.split("'>");
		if (contents.length <= 1) {
			return new String[] {};
		}
		String[] nationalities = contents[1].split("</a>");

		if (nationalities == null || nationalities.length <= 1) {
			return new String[] {};
		}
		String[] result = nationalities[0].split("/");
		for (int i = 0; i < result.length; i++) {
			result[i] = deleteWhiteSpaces(result[i]);
		}
		return result;
	}

	@Override
	public String getDescription(String p_content) {
		String[] contents = find(p_content, this.patternFirstDescription)
				.split("title");
		if (contents.length < 1) {
			return null;
		}
		String[] sortDescription = contents[1].split("</div></td>");
		if (sortDescription.length < 1) {
			return null;
		}
		String[] patternDescription = sortDescription[0].split("/>");
		if (patternDescription.length < 1) {
			return null;
		}
		String result = patternDescription[1];
		result = deleteTabulators(result);
		return result;
	}

	@Override
	public String[] getGenre(String p_content) {
		String[] contents = find(p_content, this.patternGenre).split("'>");
		if (contents.length <= 1) {
			return new String[] {};
		}
		String[] genres = contents[1].split("</a>");

		if (genres == null || genres.length <= 1) {
			return new String[] {};
		}
		String[] result = genres[0].split("/");
		for (int i = 0; i < result.length; i++) {
			result[i] = deleteWhiteSpaces(result[i]);
		}
		return result;
	}

	@Override
	public String[] getDirector(String p_content) {
		String[] parts = find(p_content, this.patternDirector).split("'>");
		ArrayList<String> resultList = new ArrayList<String>();
		if (parts.length <= 1) {
			return new String[] {};
		}
		for (String part : parts) {
			String[] directors = part.split("</a>");
			if (directors.length > 1) {
				directors[0] = deleteWhiteSpaces(directors[0]);
				resultList.add(directors[0]);
			}
		}
		return resultList.toArray(new String[resultList.size()]);
	}

	@Override
	public Integer getDuration(String p_content) {
		String[] parts = find(p_content, this.patternDuration).split("'>");
		String[] result = null;
		if (parts.length > 1) {
			result = parts[1].split(" ");

		}
		if (result != null && result.length > 1) {
			return Integer.parseInt(result[0]);
		}
		return 0;
	}

	@Override
	public String[] getCast(String p_content) {
//TODO nie dziala prawid�owo czasami nie pobiera bo roznie wyswietlaja 
		String[] temp = find(p_content, this.patternCast).split("'>");
		String[] parts = temp[2].split("</a>");
		ArrayList<String> resultList = new ArrayList<String>();
		if (parts.length <= 1) {
			return new String[] {};
		}
		String[] cast = parts[0].split("-");
		resultList.add(deleteWhiteSpaces(cast[0]));
		for (int i = 1; i < cast.length; i++) {
			cast[i] = deleteWhiteSpaces(splitCast(cast[i]));
			resultList.add(cast[i]);
		}
		return resultList.toArray(new String[resultList.size()]);
	}

	private String splitCast(String p_string) {
		String[] dsa = p_string.split("[a-z][A-Z]");
		if (dsa.length > 1) {
			int start = dsa[0].length() + 1;
			return p_string.substring(start);
		}
		return p_string;
	}

	@Override
	public Float getRating(String p_content) {
		String[] result = find(p_content, this.patternRating).split(" ");
		if (result.length < 1) {
			return null;
		}
		return Float.parseFloat(result[1]);
	}

}
